package com.sbs.gaoxinqutoilet.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.CheckToken;
import com.sbs.gaoxinqutoilet.entity.UserEntity;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constant;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.interfaces.DialogClick;
import com.sbs.gaoxinqutoilet.myapp.MyApplication;
import com.sbs.gaoxinqutoilet.utils.GsonTools;
import com.sbs.gaoxinqutoilet.utils.HttpUtils;
import com.sbs.gaoxinqutoilet.utils.JsonUtil;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.sbs.gaoxinqutoilet.utils.ToastUtils;
import com.sbs.gaoxinqutoilet.utils.loading.IosAlertDialog;
import com.sbs.gaoxinqutoilet.utils.loading.LoadingDialog;

import java.util.HashMap;

import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;


/*
*   子类是具有返回功能的fragment
*  侧滑功能fragment 继承的 base
* */
public class BaseBackFragment extends SwipeBackFragment {

    private static final String TAG = "Fragmentation";

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setParallaxOffset(0.5f);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String token_data = SPUtil.getString(Constants.KEY_TOKEN, "");
        Log.e("token","token值:"+token_data);
        //check_token
        String url_check_token= HttpApis.check_token+token_data;
//        HashMap<String, String> map = new HashMap<>();
//        map.put("token", token_data);
        HttpUtils.requestGet(getContext(), url_check_token,  new Request(){
            @Override
            public void success(String jsonStr) {
                super.success(jsonStr);
                Log.e("jsonStr","jsonStr值:"+jsonStr);
                CheckToken checkToken = JsonUtil.json2Bean(jsonStr, CheckToken.class);
                if(checkToken==null||!"1".equals(checkToken.getState())){
                    //重新登录--
                    //访问重新登录接口
                    //姓名
                    String login_name = SPUtil.getString(Constants.KEY_USERNAME, "");
                    //登录密码
                    String login_pass=SPUtil.getString(Constant.KEY_USERPWD, "");

                    getReLogin(login_name,login_pass);

                }
            }

            @Override
            public void error(String error) {
                super.error(error);
                Log.e("jsonStr","error值:"+error);
            }
        });
    }

    private void getReLogin(String login_name, String login_pass) {
        String mBuyUrl = HttpApis.login;
        HashMap<String ,String> map=new HashMap<>();
        map.put("name", login_name);
        map.put("password", login_pass);

        HttpUtils.requestPost(_mActivity, mBuyUrl, map, new Request(){
            @Override
            public void success(String jsonStr) {
                super.success(jsonStr);
                LogUtils.i("loginAccount", "success: "+jsonStr);

                UserEntity loginEntity = GsonTools.changeGsonToBean(jsonStr, UserEntity.class);
                //数据解析
                if (loginEntity != null) {
                    //姓名
                    SPUtil.putString(Constants.KEY_USERNAME, login_name);
                    //不保存登录密码
                    SPUtil.putString(Constant.KEY_USERPWD, login_pass);
                    //login  token  每次请求都要带有这个token
                    SPUtil.putString(Constants.KEY_TOKEN, loginEntity.getToken());
                    LogUtils.i("show_token_data",loginEntity.getToken());
                    MyApplication.setUserEntity(loginEntity);
                    SPUtil.putBeanValue(Constants.KEY_USER, loginEntity);

                }
                cancelLoadingDialog();
            }

            @Override
            public void error(String error) {
                super.error(error);

            }
        });
    }

    protected void initToolbarBack(Toolbar toolbar) {
        toolbar.findViewById(R.id.toolbar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _mActivity.onBackPressed();
            }
        });
        toolbar.findViewById(R.id.toolbar_iv_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(_mActivity, "点击了menu", Toast.LENGTH_SHORT).show();
            }
        });
//        toolbar.setNavigationIcon(R.drawable.titlebar_back_selector);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                _mActivity.onBackPressed();
//            }
//        });
    }
    //Toast显示
    protected void showShortToast(String string) {
        ToastUtils.showShortToast(getActivity(), string);
    }

    protected void showLoadingDialog(){
        LoadingDialog.showLoadingDialog(_mActivity);
    }

    protected void showLoadingDialog(String message){
        LoadingDialog.showLoadingDialog(_mActivity,message);
    }

    protected void cancelLoadingDialog(){
        LoadingDialog.cancelLoadingDialog();
    }

    protected void showAlertOnlyPos(Context context, String title, String msg , final DialogClick dialogClick) {
        new IosAlertDialog(context).builder()

                .setTitle(title)
                .setMsg(msg)
                .setOnlyPostivite("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogClick.PositiveMessage();
                    }
                }).show();
    }


    protected void showAlertOnlyPos(Context context, String title, String msg ) {
        new IosAlertDialog(context).builder()
                .setTitle(title)
                .setMsg(msg)
                .setOnlyPostivite("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();
    }

    protected void showAlertPos(Context context, String title, String msg, final DialogClick dialogClick) {
        new IosAlertDialog(context).builder()
                .setMsg(msg)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogClick.PositiveMessage();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogClick.NegativeMessage();
            }
        }).show();
    }

    protected void showAlertDialogNoMsg(Context context, String titleContent, String message, final DialogClick dialogClick) {

        new IosAlertDialog(context).builder().setTitle(titleContent)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogClick.PositiveMessage();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogClick.NegativeMessage();
            }
        }).show();
    }

    protected void showAlertDialog(Context context, String titleContent, String message, final DialogClick dialogClick) {

        new IosAlertDialog(context).builder().setTitle(titleContent)
                .setMsg(message)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogClick.PositiveMessage();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogClick.NegativeMessage();
            }
        }).show();
    }




//    @Override
//    public boolean onBackPressedSupport() {
//        return super.onBackPressedSupport();
//    }
}
